package Factory;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class BootstrapFactory {

    public static Bootstrap getTcpBootstrap(List<ChannelHandler> channelHandlers, InetSocketAddress inetSocketAddress) {
        Bootstrap bootstrap = new Bootstrap();
        NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup();
        bootstrap.group(nioEventLoopGroup)
                .channel(NioSocketChannel.class)
                .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, 2048000000, 2048000000))
                .remoteAddress(inetSocketAddress)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(new IdleStateHandler(120, 0, 0, TimeUnit.SECONDS));
                        channelHandlers.forEach(item -> {
                            socketChannel.pipeline().addLast(item);
                        });
                    }
                });
        return bootstrap;
    }

    public static Bootstrap getHttpBootstrap(List<ChannelHandler> channelHandlers, InetSocketAddress inetSocketAddress) {
        Bootstrap bootstrap = new Bootstrap();
        NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup();
        bootstrap.group(nioEventLoopGroup)
                .channel(NioSocketChannel.class)
                .remoteAddress(inetSocketAddress)
                .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, 2048000, 2048000))
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        // 心跳
                        socketChannel.pipeline().addLast(new IdleStateHandler(2, 0, 0, TimeUnit.SECONDS));
                        // 解码成HttpRequest
                        socketChannel.pipeline().addLast(new HttpClientCodec());
                        // 解码成HttpFullR
                        socketChannel.pipeline().addLast(new HttpObjectAggregator(2048000));
                        // 聚合
                        socketChannel.pipeline().addLast(new HttpContentDecompressor());

                        channelHandlers.forEach(item -> {
                            socketChannel.pipeline().addLast(item);
                        });
                    }
                });
        return bootstrap;
    }
}
